<?php

global $_GPC;
global $_W;
$openid = $_W['openid'];
$shopid = $_GPC['shopid'];
$tableid = $_GPC['tid'];
if (!$openid) {
    message('请使用微信登陆');
}
$memberno = pdo_fetchcolumn('SELECT id FROM ' . tablename('j_hmoney_membercard') . ' WHERE  openid =:a order by id desc limit 1', array(':a' => openid));
if (!$shopid) {
    message('请先选择店铺');
}
$cfg = $this->module['config'];
$shop = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_group') . ' WHERE id =:a', array(':a' => $shopid));
if (!$shop) {
    message('请先选择店铺');
}
isetcookie('shopid', $shopid, 300);
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
load()->model('mc');
if ($operation == 'display') {
    $goodpcatelist = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_goodcategory') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid =\'' . $shopid . '\' order by  displayorder asc, id desc');
    if ($tableid) {
        $trade = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE tableid =:a and status<=2 and ispay=0 order by id desc limit 1', array(':a' => $tableid));
        if (!$trade) {
            message('该台不可下单,请联系服务员');
        }
    }
} else {
    if ($operation == 'page_submit') {
        if ($tableid) {
            $table = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_table') . ' WHERE id=:a ', array(':a' => $tableid));
        }
        $orderAry = $_GPC['__cookie_orderary'];
        if (strlen($orderAry) < 5) {
            header('location:' . $this->createMobileUrl('placeorder', array('op' => 'display')));
        }
        $ordergoodlist = explode('|**|', $orderAry);
        $tempary_gid = array();
        $tempary = array();
        foreach ($ordergoodlist as $row) {
            $temp = explode('|*|', $row);
            $tempary_gid[] = $temp[0];
            $tempary[] = array('gid' => $temp[0], 'num' => $temp[1], 'taste' => $temp[2], 'title' => $temp[3]);
        }
        $goodlist = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_goods') . ' WHERE id in(' . implode(',', $tempary_gid) . ') order by id desc ');
        $goodary = array();
        foreach ($goodlist as $row) {
            $goodary[$row['id']] = $memberno ? intval($row['memberprice'] * 100) : ($memberno ? intval($row['memberprice'] * 100) : intval($row['marketprice'] * 100));
        }
        $totalfee = 0;
        $num = 0;
        foreach ($tempary as $row) {
            intval($row['num']);
            $totalfee += $goodary[$row['gid']] * intval($row['num']);
            $num += intval($row['num']);
        }
        $totalfee = $this->_2money($totalfee);
    } else {
        if ($operation == 'getgoodlist') {
            $cid = intval($_GPC['cid']);
            $goodlist = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_goods') . ' WHERE pcate=:a order by id desc ', array(':a' => $cid));
            $i = 0;
            while ($i < count($goodlist)) {
                if ($goodlist[$i]['thumb']) {
                    $goodlist[$i]['thumb'] = tomedia($goodlist[$i]['thumb']);
                }
                $i = $i + 1;
            }
            exit(json_encode(array('success' => true, 'list' => $goodlist)));
        } else {
            if ($operation == 'getgood') {
                $id = intval($_GPC['id']);
                $item = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_goods') . ' WHERE  id =:a ', array(':a' => $id));
                $item['thumb'] = !$item['thumb'] ? '' : tomedia($item['thumb']);
                $salenum = pdo_fetchcolumn('SELECT sum(num) FROM ' . tablename('j_hmoney_ordergoods') . ' WHERE  goodid =\'' . $id . '\' and ispay=1 and createtime>=' . strtotime(date('Y-m') . '-01 00:00:00'));
                $item['sale'] = $salenum;
                exit(json_encode(array('success' => true, 'item' => $item)));
            } else {
                if ($operation == 'updateordergoods') {
                    $orderAry = $_GPC['orderAry'];
                    isetcookie('__cookie_orderary', '');
                    isetcookie('__cookie_orderary', $orderAry, 1800);
                    exit(json_encode(array('success' => true)));
                } else {
                    if ($operation == 'scantable') {
                        $code = urldecode($_GPC['code']);
                        $item = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_table') . ' WHERE qrlink =:a and groupid=:b ', array(':a' => $code, ':b' => $shopid));
                        if (!$code || !$item) {
                            exit(json_encode(array('success' => false, 'msg' => '二维码错误')));
                        }
                        if ($item['status'] < 1 || $item['status'] > 2) {
                            exit(json_encode(array('success' => false, 'msg' => '该餐台还没有开台，请联系服务员进行开台')));
                        }
                        $trade = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE tableid =:a and status<=2 and ispay=0 order by id desc limit 1', array(':a' => $item['id']));
                        if (!$trade) {
                            exit(json_encode(array('success' => false, 'msg' => '该台不可下单')));
                        }
                        $tablepcatename = pdo_fetchcolumn('SELECT title FROM ' . tablename('j_hmoney_tablecategory') . ' WHERE  id =\'' . $item['pcate'] . '\'');
                        $item['tablename'] = $tablepcatename . '-' . $item['title'];
                        exit(json_encode(array('success' => true, 'item' => $item)));
                    } else {
                        if ($operation == 'submitorder') {
                            $tableid = $_GPC['tableid'];
                            $table = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_table') . ' WHERE id =:a ', array(':a' => $tableid));
                            if (!$table) {
                                exit(json_encode(array('success' => false, 'msg' => '二维码错误')));
                            }
                            if ($table['status'] < 1 || $table['status'] > 2) {
                                exit(json_encode(array('success' => false, 'msg' => '该餐台还没有开台，请联系服务员进行开台')));
                            }
                            $trade = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE tableid =:a and status<=2 and ispay=0 order by id desc limit 1', array(':a' => $table['id']));
                            if (!$trade) {
                                exit(json_encode(array('success' => false, 'msg' => '该餐台还没有开台，请联系服务员进行开台')));
                            }
                            isetcookie('page_cookie', 1);
                            $orderAry = $_GPC['__cookie_orderary'];
                            if (strlen($orderAry) < 5) {
                                header('location:' . $this->createMobileUrl('placeorder', array('op' => 'display')));
                            }
                            $ordergoodlist = explode('|**|', $orderAry);
                            $tempary_gid = array();
                            $tempary = array();
                            $sqlinsert = 'insert into ' . tablename('j_hmoney_ordergoods') . ' (weid,groupid,orderid,createtime,out_trade_no,openid,goodid,goodsn,title,price,oldprice,num,total_fee,status,taste)VALUES';
                            foreach ($ordergoodlist as $row) {
                                $temp = explode('|*|', $row);
                                $tempary_gid[] = $temp[0];
                                $tempary[] = array('gid' => $temp[0], 'num' => $temp[1], 'taste' => $temp[2], 'title' => $temp[3]);
                            }
                            $goodlist = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_goods') . ' WHERE id in(' . implode(',', $tempary_gid) . ') order by id desc ');
                            $goodary = array();
                            foreach ($goodlist as $row) {
                                $goodary[$row['id']] = $row;
                            }
                            $tempInsertAry = array();
                            foreach ($tempary as $row) {
                                $temp = $goodary[$row['gid']];
                                $price = $memberno ? intval($temp['memberprice'] * 100) : ($memberno ? intval($temp['memberprice'] * 100) : intval($temp['marketprice'] * 100));
                                $totalfee = $price * $row['num'];
                                $this->_2money($price);
                                $tempInsertAry[] = '(\'' . implode('\',\'', array(0 => $_W['uniacid'], 1 => $shopid, 2 => $trade['id'], 3 => TIMESTAMP, 4 => $trade['out_trade_no'], 5 => $openid, 6 => $temp['id'], 7 => $temp['goodsn'], 8 => $temp['title'], 9 => $this->_2money($price), 10 => $temp['marketprice'], 11 => $row['num'], 12 => $totalfee, 13 => 1, 14 => $row['taste'])) . '\')';
                            }
                            if (count($tempInsertAry)) {
                                pdo_run($sqlinsert . implode(',', $tempInsertAry) . ';');
                            }
                            pdo_update('j_hmoney_table', array('status' => 2, 'newctrl' => 1), array('id' => $trade['tableid']));
                            $gather = pdo_fetch('SELECT sum(num) as num1,count(*) as num2,sum(total_fee) as tfee,sum(oldprice*num*100) as ofee FROM ' . tablename('j_hmoney_ordergoods') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and orderid=:a ', array(':a' => $trade['id']));
                            $gather2 = pdo_fetch('SELECT sum(a.total_fee) as notfee FROM ' . tablename('j_hmoney_ordergoods') . ' a left join ' . tablename('j_hmoney_goods') . ' b on a.goodid=b.id WHERE a.weid = \'' . $_W['uniacid'] . '\' and b.isnotcoupon=1 and a.out_trade_no=:a ', array(':a' => $trade['out_trade_no']));
                            $data = array('order_fee' => $gather['tfee'], 'total_fee' => $gather['tfee'], 'notdiscount_fee' => $gather2['notfee'], 'num' => $gather['num1'], 'species' => $gather['num2'], 'status' => 1);
                            pdo_update('j_hmoney_order', $data, array('id' => $trade['id']));
                            $insertdata = array('shopid' => $shopid, 'orderid' => $trade['id'], 'mtype' => 'addgood', 'message' => '[' . date('m-d H:i') . '][' . $table['title'] . ']已下单，请及时处理。');
                            _sendWebMessage($insertdata);
                            $this->wildDog_socket($shopid);
                            isetcookie('__cookie_orderary', '');
                            exit(json_encode(array('success' => true, 'orderid' => $trade['id'])));
                        } else {
                            if ($operation == 'memberorderview') {
                                $orderid = $_GPC['orderid'];
                                $trade = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE id =:a ', array(':a' => $orderid));
                                $ordergoods = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_ordergoods') . ' WHERE orderid =:a order by id desc', array(':a' => $orderid));
                                $ordershop = pdo_fetchcolumn('SELECT companyname FROM ' . tablename('j_hmoney_group') . ' WHERE id =:a ', array(':a' => $trade['groupid']));
                                $table = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_table') . ' WHERE id =:a ', array(':a' => $trade['tableid']));
                                $tablepcatename = pdo_fetchcolumn('SELECT title FROM ' . tablename('j_hmoney_tablecategory') . ' WHERE  id =\'' . $table['pcate'] . '\'');
                                $selffee = pdo_fetchcolumn('SELECT sum(total_fee) FROM ' . tablename('j_hmoney_ordergoods') . ' WHERE orderid =:a and openid=:b ', array(':a' => $orderid, ':b' => $openid));
                            } else {
                                if ($operation == 'memberorderlist') {
                                    $shoplist = pdo_fetchall('SELECT id,companyname FROM ' . tablename('j_hmoney_group') . '  WHERE weid=:a order by id desc ', array(':a' => $_W['uniacid']));
                                    $shopary = array();
                                    foreach ($shoplist as $row) {
                                        $shopary[$row['id']] = $row['companyname'];
                                    }
                                    $tablelist = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_table') . ' WHERE groupid =:a ', array(':a' => $shopid));
                                    $tableAry = array();
                                    foreach ($tablelist as $row) {
                                        $tableAry[$row['id']] = $row['title'];
                                    }
                                    $pindex = max(1, intval($_GPC['page']));
                                    $psize = 5;
                                    $orderlist = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE openid=:a or id in(SELECT orderid FROM ' . tablename('j_hmoney_ordergoods') . ' WHERE openid=:a group by orderid) order by id desc LIMIT ' . ($pindex - 1) * $psize . ',' . $psize, array(':a' => $openid));
                                    $tempary = array();
                                    foreach ($orderlist as $rew) {
                                        $tempary[] = $rew['id'];
                                    }
                                    $orderAry = array();
                                    $goodslist = pdo_fetchall('SELECT a.orderid,b.title,b.thumb FROM ' . tablename('j_hmoney_ordergoods') . ' a left join ' . tablename('j_hmoney_goods') . ' b on a.goodid=b.id  WHERE a.orderid in (' . implode(',', $tempary) . ') order by a.orderid desc', array(':a' => $openid));
                                    foreach ($goodslist as $rew) {
                                        if (!isset($orderAry[$rew['orderid']])) {
                                            $orderAry[$rew['orderid']] = array();
                                        }
                                        $orderAry[$rew['orderid']][] = $rew;
                                    }
                                    $total = pdo_fetchcolumn('SELECT count(*) FROM ' . tablename('j_hmoney_order') . ' WHERE openid=:a or id in(SELECT orderid FROM ' . tablename('j_hmoney_ordergoods') . ' WHERE openid=:a group by orderid) order by id desc ', array(':a' => $openid));
                                    $pager = pagination($total, $pindex, $psize);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
include $this->template($cfg['floder'] . '/phone/placeorder');